<?php
load_theme_textdomain( 'a16006' );
if ( function_exists('register_sidebar') ){
	register_sidebar();
	register_sidebar( array(
    	'name'          => __('Footbar'),
    	'id'            => 'footbar'));

	register_sidebar( array(
    	'name'          => __('Home Items'),
    	'id'            => 'home_items'));
}

/* next is init config panel fields */
$themename = "A16006-A";
$shortname = "a16006a";
$options = array (
array("name" => "Welcome Message",
        "type" => "title"),
array("type" => "open"),
//--- ok start fields ---
array(    "name" => "Navigation Type",
        "desc" => "Set up the type of navigation menu on page header, can be  \"category\" or \"page\".",
        "id" => $shortname."_nav_type",
        "std" => "category",
        "type" => "text"),
array(    "name" => "Navigation Items",
        "desc" => "Set up items shown in navigation bar, splited by comma",
        "id" => $shortname."_nav_items",
        "std" => "",
        "type" => "textarea"),
array(    "name" => "Feature Tags",
        "desc" => "When a post's tags meet one of these tags, this post's image will show in home page.",
        "id" => $shortname."_feature_tags",
        "std" => "feature",
        "type" => "text"),
array(    "type" => "close")
);

/**
 * WordPress standard function , initializ theme option panel.
 * @return unknown_type
 */
function mytheme_add_admin(){
	global $themename, $shortname, $options;

	if ( $_GET['page'] == basename(__FILE__) ) {
		if ( 'save' == $_REQUEST['action'] ) {
			foreach ($options as $value) {
				update_option( $value['id'], $_REQUEST[ $value['id'] ] );
			}

			foreach ($options as $value) {
				if( isset( $_REQUEST[ $value['id'] ] ) ) {
					update_option( $value['id'], $_REQUEST[ $value['id'] ]  );
				} else {
					delete_option( $value['id'] );
				}
			}
			header("Location: themes.php?page=functions.php&saved=true");
			die;
		} else if( 'reset' == $_REQUEST['action'] ) {
			foreach ($options as $value) {
				delete_option( $value['id'] );
			}
			header("Location: themes.php?page=functions.php&reset=true");
			die;

		}
	}

	add_theme_page($themename." Options", "".$themename." Options", 'edit_themes', basename(__FILE__), 'mytheme_admin');

}

/**
 * WordPress standar function
 * @return unknown_type
 */
function mytheme_admin() {

	global $themename, $shortname, $options;

	if ( $_REQUEST['saved'] ) echo '<div id="message" class="updated fade"><p><strong>'.$themename.' settings saved.</strong></p></div>';
	if ( $_REQUEST['reset'] ) echo '<div id="message" class="updated fade"><p><strong>'.$themename.' settings reset.</strong></p></div>';

	?>
<div class="wrap">
<h2><?php echo $themename; ?> Settings</h2>

<form method="post"><?php foreach ($options as $value) {
	switch ( $value['type'] ) {

		case "open":
			?>
<table width="100%" border="0"
	style="background-color: #eef5fb; padding: 10px;">

	<?php break;

case "close":
	?>

</table>
<br />

	<?php break;

case "title":
	?>
<table width="100%" border="0"
	style="background-color: #dceefc; padding: 5px 10px;">
	<tr>
		<td colspan="2">
		<h3 style="font-family: Georgia, 'Times New Roman', Times, serif;"><?php echo $value['name']; ?></h3>
		</td>
	</tr>

	<?php break;

case 'text':
	?>

	<tr>
		<td width="20%" rowspan="2" valign="middle"><strong><?php echo $value['name']; ?></strong></td>
		<td width="80%"><input style="width: 400px;"
			name="<?php echo $value['id']; ?>" id="<?php echo $value['id']; ?>"
			type="<?php echo $value['type']; ?>"
			value="<?php if ( get_settings( $value['id'] ) != "") { echo get_settings( $value['id'] ); } else { echo $value['std']; } ?>" /></td>
	</tr>

	<tr>
		<td><small><?php echo $value['desc']; ?></small></td>
	</tr>
	<tr>
		<td colspan="2"
			style="margin-bottom: 5px; border-bottom: 1px dotted #000000;">&nbsp;</td>
	</tr>
	<tr>
		<td colspan="2">&nbsp;</td>
	</tr>

	<?php
	break;

case 'textarea':
	?>

	<tr>
		<td width="20%" rowspan="2" valign="middle"><strong><?php echo $value['name']; ?></strong></td>
		<td width="80%"><textarea name="<?php echo $value['id']; ?>"
			style="width: 400px; height: 200px;"
			type="<?php echo $value['type']; ?>" cols="" rows=""><?php if ( get_settings( $value['id'] ) != "") { echo get_settings( $value['id'] ); } else { echo $value['std']; } ?></textarea></td>

	</tr>

	<tr>
		<td><small><?php echo $value['desc']; ?></small></td>
	</tr>
	<tr>
		<td colspan="2"
			style="margin-bottom: 5px; border-bottom: 1px dotted #000000;">&nbsp;</td>
	</tr>
	<tr>
		<td colspan="2">&nbsp;</td>
	</tr>

	<?php
	break;

case 'select':
	?>
	<tr>
		<td width="20%" rowspan="2" valign="middle"><strong><?php echo $value['name']; ?></strong></td>
		<td width="80%"><select style="width: 240px;"
			name="<?php echo $value['id']; ?>" id="<?php echo $value['id']; ?>">
			<?php foreach ($value['options'] as $option) { ?>
			<option
			<?php if ( get_settings( $value['id'] ) == $option) { echo ' selected="selected"'; } elseif ($option == $value['std']) { echo ' selected="selected"'; } ?>><?php echo $option; ?></option>
			<?php } ?>
		</select></td>
	</tr>

	<tr>
		<td><small><?php echo $value['desc']; ?></small></td>
	</tr>
	<tr>
		<td colspan="2"
			style="margin-bottom: 5px; border-bottom: 1px dotted #000000;">&nbsp;</td>
	</tr>
	<tr>
		<td colspan="2">&nbsp;</td>
	</tr>

	<?php
	break;

case "checkbox":
	?>
	<tr>
		<td width="20%" rowspan="2" valign="middle"><strong><?php echo $value['name']; ?></strong></td>
		<td width="80%"><?php if(get_option($value['id'])){ $checked = "checked=\"checked\""; }else{ $checked = "";} ?>
		<input type="checkbox" name="<?php echo $value['id']; ?>"
			id="<?php echo $value['id']; ?>" value="true" <?php echo $checked; ?> />
		</td>
	</tr>

	<tr>
		<td><small><?php echo $value['desc']; ?></small></td>
	</tr>
	<tr>
		<td colspan="2"
			style="margin-bottom: 5px; border-bottom: 1px dotted #000000;">&nbsp;</td>
	</tr>
	<tr>
		<td colspan="2">&nbsp;</td>
	</tr>

	<?php break;

	}
}
?>

	<p class="submit"><input name="save" type="submit" value="Save changes" />
	<input type="hidden" name="action" value="save" /></p>
	</form>
	<form method="post">
	<p class="submit"><input name="reset" type="submit" value="Reset" /> <input
		type="hidden" name="action" value="reset" /></p>
	</form>

	<?php
}

// register theme's option panel to admin control panel.
add_action('admin_menu', 'mytheme_add_admin');

/**
 * initialization theme option's variables.
 * if needed ,put this function in the head of index.php,sidebar.php,single.php or other.
 * @return unknown_type
 */
function initThemeVariables(){
	global $options;
	foreach ($options as $value) {
		if (get_settings( $value['id'] ) === FALSE) {
			global $$value['id'];
			$$value['id'] = $value['std'];
		} else {
			global $$value['id'];
			$$value['id'] = get_settings( $value['id'] );
		}
	}

}

/**
 * Register initThemeVariables to the WordPress head.
 * When a page started , initializ those theme variables first.
 */
add_action('wp_head','initThemeVariables');

/**
 * Get the newest feature article.
 * @param $number
 * @return unknown_type
 */
function lastFeature($number = 1){
	global $a16006a_feature_tags;
	if($a16006a_feature_tags===FALSE){
		$a16006a_feature_tags="poster";
	}
	$args = array(
	'numberposts' => $number,
	'tag' => $a16006a_feature_tags,
	'orderby'=>"date"
	);
	$rand_posts = get_posts($args);
	return $rand_posts;
}

/**
 * Check if an article is set as 'poster'.
 * @param $posttags The article object for checked.
 * @return return true , if the article was set as 'poster'. Else return false ;
 */
function isFeature($posttags){
	global $a16006a_feature_tags;
	if($a16006a_feature_tags===FALSE){
		$a16006a_feature_tags="feature";
	}
	$tags = preg_split("/[;,]+/",$a16006a_feature_tags,-1,PREG_SPLIT_NO_EMPTY);
	if ($posttags) {
		foreach($posttags as $tag) {
			foreach($tags as $poster){
				if($tag->name == $poster) return true;
			}
		}
	}
	return false;
}



/* (thumbnail, medium, large or full) */
function the_image($size = 'medium' , $class = ''){
	global $post;
	//setup the attachment array
	$att_array = array(
		'numberposts' => 1,
		'post_parent' =>$post->ID,
		'post_type' =>'attachment',
		'post_mime_type'=> 'image',
		'order_by'=>'menu_order',
		'order'=>'ASC'
		);

		//get the post attachments
		$attachments = get_children($att_array);

		//make sure there are attachments
		if (is_array($attachments)){

			//loop through them
			foreach($attachments as $att){
				//find the one we want based on its characteristics
				if ( $att->menu_order == 0){
					$image_src_array = wp_get_attachment_image_src($att->ID, $size);

					//get url - 1 and 2 are the x and y dimensions
					$url = $image_src_array[0];
					$caption = $att->post_excerpt;
					$image_html = '<img class="%s" src="%s" alt="%s" />';
					//combine the data
					$html = sprintf($image_html,$class,$url,$caption);
					//echo the result
					echo $html;
				}
			}
		}

}




function initNavigations(){
	global $a16006a_nav_type;
	global $a16006a_nav_items;
	if(empty($a16006a_nav_items))return;
	$navItems = preg_split("/[;,]+/",$a16006a_nav_items,-1,PREG_SPLIT_NO_EMPTY);
	if($a16006a_nav_type == 'page'){
		foreach($navItems as $item){
			
			$page = get_page_by_title($item);
			if(!($page===FALSE)){
				echo'<li id="page_'.$page->ID.'"><a href="'.get_bloginfo('url').'/'.get_page_uri( $page->ID ).'">'.$page->post_title.'</a></li>';
			}
		}
	}else{ // default as category
			foreach($navItems as $item){
				$cat = get_category_by_slug($item); 
				if(!($cat===FALSE)){
				echo'<li id="cat_'.$cat->category_nicename.'"><a href="'.get_bloginfo('url').'/category/archives/'.$cat->category_nicename.'">'.$cat->cat_name.'</a></li>';
				}
		}
	}
}

/**
 * Posts widget class
 *
 * @since 2.8.0
 */
class A16006_Widget_Posts extends WP_Widget {
	function A16006_Widget_Posts() {
		$widget_ops = array('description' => __( 'Display posts by category') );
		$this->WP_Widget('posts',__('Posts List'), $widget_ops);
	}

	function widget( $args, $instance ) {
		extract( $args );
		$category = empty( $instance['category'] ) ? '' : $instance['category'];
		
		if(!empty($category)){
			$cat_obj= get_category($category );
		}
		$title = apply_filters('widget_title', empty( $instance['title'] ) ? $cat_obj->cat_name : $instance['title']);
		
		$sortby = empty( $instance['sortby'] ) ? 'date' : $instance['sortby'];
		$numberposts = empty( $instance['numberposts'] ) ? 5 : $instance['numberposts'];
		

		if ( $sortby == 'date' )
		$sortby = 'date, title';
		$posts_arg=array(
			'numberposts' => $numberposts,
			'category' => $category,
			'orderby' =>$sortby,
			);
		$before_widget = preg_replace('/id="[^"]*"/','id="category_'.$cat_obj->category_nicename.'"', $before_widget);
		echo $before_widget;
		?>
		
		<h2><?php echo $title ;?></h2>
		<ul>
 		<?php
 		global $post;
 		$myposts = get_posts($posts_arg);
 		foreach($myposts as $post) :
 		?>
    		<li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
 		<?php endforeach; ?>
 		</ul> 
		
		<?php 
		echo $after_widget;
	}


	function update( $new_instance, $old_instance ) {
		$instance = $old_instance;
		$instance['title'] = strip_tags($new_instance['title']);
		/*
		if ( in_array( $new_instance['sortby'], array( 'post_title', 'menu_order', 'ID' ) ) ) {
			$instance['sortby'] = $new_instance['sortby'];
		} else {
			$instance['sortby'] = 'menu_order';
		}

		$instance['exclude'] = strip_tags( $new_instance['exclude'] );*/
		$instance['category']=strip_tags( $new_instance['category']);
		$instance['numberposts']=strip_tags( $new_instance['numberposts']);
		$instance['sortby']=strip_tags( $new_instance['sortby']);
		return $instance;
	}

	function form( $instance ) {
		//Defaults
		$instance = wp_parse_args( (array) $instance, array( 'sortby' => 'post_title', 'title' => '', 'exclude' => '') );
		$title = esc_attr( $instance['title'] );
		$category = esc_attr( $instance['category'] );
		$sortby = esc_attr( $instance['sortby'] );
		?>
	<p><label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:'); ?></label>
	<input class="widefat" id="<?php echo $this->get_field_id('title'); ?>"
		name="<?php echo $this->get_field_name('title'); ?>" type="text"
		value="<?php echo $title; ?>" /></p>
	<p><label for="<?php echo $this->get_field_id('sortby'); ?>"><?php _e( 'Sort by:' ); ?></label>
	<select name="<?php echo $this->get_field_name('sortby'); ?>"
		id="<?php echo $this->get_field_id('sortby'); ?>" class="widefat">
		<option value="post_title"
		<?php selected( $instance['sortby'], 'date' ); ?>><?php _e('Date'); ?></option>
		<option value="menu_order"
		<?php selected( $instance['sortby'], 'title' ); ?>><?php _e('Post Title'); ?></option>
	</select></p>
	<?php 
	/*
	<p><label for="<?php echo $this->get_field_id('exclude'); ?>"><?php _e( 'Exclude:' ); ?></label>
	<input type="text" value="<?php echo $exclude; ?>"
		name="<?php echo $this->get_field_name('exclude'); ?>"
		id="<?php echo $this->get_field_id('exclude'); ?>" class="widefat" />
	<br />
	<small><?php _e( 'Page IDs, separated by commas.' ); ?></small></p>
	*/?>
	<p>
	<label for="<?php echo $this->get_field_id('category'); ?>"><?php _e( 'Category' ); ?></label>
	<?php  wp_dropdown_categories( array( 'hierarchical'       => 1,
		'name'               => $this->get_field_name('category'),
		'class'              => 'widefat',
		'selected'           => $category
		 )); ?>
	</p>
	<?php
	}

}

/**
 * Social network widget class
 *
 * @since 2.8.0
 */
class A16006_Widget_Social extends WP_Widget {
	function A16006_Widget_Social() {
		$widget_ops = array('description' => __( 'Display your social networks') );
		$this->WP_Widget('social',__('Social Network'), $widget_ops);
	}

	function widget( $args, $instance ) {
		extract( $args );
		$category = empty( $instance['category'] ) ? '' : $instance['category'];
		
		
		echo $before_widget;
		?>
		
		<h2><?php echo __('Social Network','a16006') ;?></h2>
		<ul>
 		<?php
 		if(!empty( $instance['rss'] )){
 		?>
    		<li id="social_rss"><a href="<?php echo $instance['rss']; ?>">RSS</a></li>
 		<?php }else{?>
 			<li id="social_rss"><a href="<?php bloginfo('rss2_url'); ?>">RSS</a></li>
 		<?php }?>
 		<?php
 		if(!empty( $instance['twitter'] )){
 		?>
    		<li id="social_twitter"><a href="<?php echo $instance['twitter']; ?>">Twitter</a></li>
 		<?php }?>
 		<?php
 		if(!empty( $instance['facebook'] )){
 		?>
    		<li id="social_facebook"><a href="<?php echo $instance['facebook']; ?>">Facebook</a></li>
 		<?php }?>
 		<?php
 		if(!empty( $instance['email'] )){
 		?>
    		<li id="social_email"><a href="mailto:<?php echo $instance['email']; ?>">Email</a></li>
 		<?php }?>
 		</ul> 
		
		<?php 
		echo $after_widget;
	}


	function update( $new_instance, $old_instance ) {
		$instance = $old_instance;
		$instance['rss'] = strip_tags($new_instance['rss']);
		/*
		if ( in_array( $new_instance['sortby'], array( 'post_title', 'menu_order', 'ID' ) ) ) {
			$instance['sortby'] = $new_instance['sortby'];
		} else {
			$instance['sortby'] = 'menu_order';
		}

		$instance['exclude'] = strip_tags( $new_instance['exclude'] );*/
		$instance['facebook']=strip_tags( $new_instance['facebook']);
		$instance['email']=strip_tags( $new_instance['email']);
		$instance['twitter']=strip_tags( $new_instance['twitter']);
		return $instance;
	}

	function form( $instance ) {
		//Defaults
		$rss = esc_attr( $instance['rss'] );
		$twitter = esc_attr( $instance['twitter'] );
		$facebook = esc_attr( $instance['facebook'] );
		$email = esc_attr( $instance['email'] );
		?>
	<p><label for="<?php echo $this->get_field_id('rss'); ?>"><?php _e('RSS:'); ?></label>
	<input class="widefat" id="<?php echo $this->get_field_id('rss'); ?>"
		name="<?php echo $this->get_field_name('rss'); ?>" type="text"
		value="<?php echo $rss; ?>" /></p>
	<p><label for="<?php echo $this->get_field_id('twitter'); ?>"><?php _e('Twitter:'); ?></label>
	<input class="widefat" id="<?php echo $this->get_field_id('twitter'); ?>"
		name="<?php echo $this->get_field_name('twitter'); ?>" type="text"
		value="<?php echo $twitter; ?>" /></p>
	<p><label for="<?php echo $this->get_field_id('facebook'); ?>"><?php _e('Facebook:'); ?></label>
	<input class="widefat" id="<?php echo $this->get_field_id('facebook'); ?>"
		name="<?php echo $this->get_field_name('facebook'); ?>" type="text"
		value="<?php echo $facebook; ?>" /></p>
	<p><label for="<?php echo $this->get_field_id('email'); ?>"><?php _e('Email:'); ?></label>
	<input class="widefat" id="<?php echo $this->get_field_id('email'); ?>"
		name="<?php echo $this->get_field_name('email'); ?>" type="text"
		value="<?php echo $email; ?>" /></p>
	
	<?php 
	/*
	<p><label for="<?php echo $this->get_field_id('exclude'); ?>"><?php _e( 'Exclude:' ); ?></label>
	<input type="text" value="<?php echo $exclude; ?>"
		name="<?php echo $this->get_field_name('exclude'); ?>"
		id="<?php echo $this->get_field_id('exclude'); ?>" class="widefat" />
	<br />
	<small><?php _e( 'Page IDs, separated by commas.' ); ?></small></p>
	*/?>
	<?php
	}

}
add_action('widgets_init', 'a16006_widget_init');
function a16006_widget_init() {
	register_widget('A16006_Widget_Posts');
	register_widget('A16006_Widget_Social');
}

?>